package com.course.cdspringboot.controller;


import com.course.cdspringboot.mapper.TeacherMapper;
import com.course.cdspringboot.security.RoleAccessControl;
import com.course.cdspringboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api/teacher")
public class TeacherController {

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private RoleAccessControl roleAccessControl;

    // 获取所有老师的列表
    @GetMapping("/all_teachers")
    public List<Map<String, Object>> getAllTeachers() {
        roleAccessControl.checkAccess("ROLE_ADMIN");
        return teacherMapper.getAllTeachers();
    }

    // 老师获取自己的主页信息
    @GetMapping("/teacher_home_info")
    public Map<String, Object> getTeacherInfo() {
        Long uid = roleAccessControl.checkAccess("ROLE_TEACHER");
        return teacherMapper.teacherHomeInfo((Long) uid);
    }

    // 老师更新自己的详细信息
    @PatchMapping("/update_personal_info")
    public Result updatePersonalInfo(@RequestBody Map<String, Object> teacherDetailInfo){
        Long uid = roleAccessControl.checkAccess("ROLE_TEACHER");
        teacherDetailInfo.put("uid", uid);
        int change = teacherMapper.updatePersonalInfo(teacherDetailInfo);
        if (change > 0) {
            return Result.update_ok();
        }
        return Result.update_error().data("msg", "更新失败");
    }



}